# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : 批量确认（BlockingConnection 模式）.py
# @Author: dongguangwen
# @Date  : 2025-04-12 15:40
import pika

# 建立连接和通道
credentials = pika.PlainCredentials('admin', 'password')
connection = pika.BlockingConnection(
    pika.ConnectionParameters('192.168.1.119', credentials=credentials, virtual_host='my_vhost'))
channel = connection.channel()

# 声明交换器和队列
channel.exchange_declare(exchange='batch_confirm_ex', exchange_type='direct', durable=True)
channel.queue_declare(queue='batch_confirm_q', durable=True)
channel.queue_bind(exchange='batch_confirm_ex', queue='batch_confirm_q', routing_key='batch_key')

# ✅ 开启发送确认模式（同步）
channel.confirm_delivery()

# 批量发送 10 条消息
for i in range(10):
    msg = f"批量消息 {i}"
    channel.basic_publish(
        exchange='batch_confirm_ex',
        routing_key='batch_key',
        body=msg,
        properties=pika.BasicProperties(delivery_mode=2)
    )
    print(f"[→] 发送消息：{msg}")

# 没有报错即代表全部被确认
print("[✅] 所有消息已被 RabbitMQ Broker 批量确认")
connection.close()
